package org.budo.poi.util;

import java.io.File;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * @author lmw
 */
@Getter
@Setter
@Accessors(fluent = true)
public class SheetBuilder {
    private ExcelBuilder excelBuilder;

    private String name;

    private Map<String, Object> header;

    private List<?> list;

    private DataProducer dataProducer;

    public SheetBuilder(String name, ExcelBuilder excelBuilder) {
        this.name = name;
        this.excelBuilder = excelBuilder;
    }

    public SheetBuilder list(DataProducer dataProducer) {
        this.dataProducer=dataProducer;
        return this;
    }

    String[] headerNames() {
        Map<String, Object> header = this.header;
        if (null == header || header.isEmpty()) {
            throw new IllegalArgumentException("#93 header is null, header=" + header + ", this=" + this);
        }

        Set<String> keys = header.keySet();
        return keys.toArray(new String[keys.size()]);
    }

    public SheetBuilder sheet(String sheetName) {
        return this.excelBuilder().sheet(sheetName);
    }

    public InputStream toInputStream() {
        return this.excelBuilder().toInputStream();
    }

    public void toFile(File file) {
        this.excelBuilder().toFile(file);
    }

    public void download(HttpServletResponse response, String fileName) {
        this.excelBuilder().download(response, fileName);
    }
}